# Maintainer: Antoine Martin <totaam@xpra.org>
# Contributor: Miloš Komarčević <miloskomarcevic@aim.com>

_realname=libyuv
pkgbase=mingw-w64-${_realname}
pkgname=(${MINGW_PACKAGE_PREFIX}-${_realname})
provides=(${MINGW_PACKAGE_PREFIX}-${_realname}-git)
conflicts=(${MINGW_PACKAGE_PREFIX}-${_realname}-git)
replaces=(${MINGW_PACKAGE_PREFIX}-${_realname}-git)
pkgver=1909.r2832.4db2af62d
pkgrel=1
pkgdesc="YUV conversion and scaling library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://chromium.googlesource.com/libyuv/libyuv"
license=('spdx:BSD-3-Clause')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc"
             'git')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-libjpeg")
_commit="4db2af62dab48895226be6b52737247e898ebe36"
source=("${_realname}::git+https://chromium.googlesource.com/${_realname}/${_realname}#commit=${_commit}"
        "libyuv.pc.in"
        "pkgconfig-cmake.patch")
sha256sums=('SKIP'
            '86aaa22252e033e62931e7c3fe3a6be4143a8ca532f95c1346b1e6b278c1947f'
            'b7fc89b8a8da6b3dd2165b9be3408545b3e6ec6e7cae34a50843c5a24f8313c0')

pkgver() {
  cd "${srcdir}"/${_realname}
  local VERSION=$(cat include/libyuv/version.h | grep "#define LIBYUV_VERSION" | awk '{print $3}')

  printf "%s.r%s.%s" "${VERSION}" "$(git rev-list --count $_commit)" "$(git rev-parse --short $_commit)"
}

prepare() {
  cd "${srcdir}"/${_realname}
  cp "${srcdir}"/libyuv.pc.in "${srcdir}"/${_realname}
  patch -p1 -i "${srcdir}"/pkgconfig-cmake.patch
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      ../${_realname}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  install -Dm644 "${srcdir}"/${_realname}/LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
